package com.diqizu.servlet.admin;



import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import com.diqizu.entity.DesignerEntity;

import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.DBManager;
import com.diqizu.utli.Result;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;



import com.diqizu.dao.SheJiDao;
import com.diqizu.entity.DesignerEntity;
import com.diqizu.entity.IntroDuctionEntity;
import com.diqizu.entity.MessaGeEntity;
import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.DBManager;
import com.diqizu.utli.Result;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**
 * Servlet implementation class AboutServletadmin
 */
@WebServlet("/admin/AboutServletadmin")
public class AboutServletadmin extends BaseServlet {
	SheJiDao dao = new SheJiDao();

	// 公司介绍修改
	public void intsertgongsi(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String content = request.getParameter("content");
		String content2 = request.getParameter("content2");
		String gourl = request.getParameter("gourl");
		String content3 = request.getParameter("content3");
		String content4 = request.getParameter("content4");
		String gourl2 = request.getParameter("gourl2");

		boolean aa = dao.addIntroduction(new IntroDuctionEntity(0, content, content2, gourl, content3, content4, gourl2));
		PrintWriter out = response.getWriter();
		if (aa) {
			out.write(Result.toClient(0, "成功"));
		} else {
			out.write(Result.toClient(1, "失败"));
		}

		out.flush();
		out.close();
		// request.getRequestDispatcher("about.jsp").forward(request, response);

	}

	// 获取‘公司简介’页面的信息
	public void dogongsi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取‘公司介绍’的列表
		ArrayList<IntroDuctionEntity> introduce = dao.Introduction();

		String res = Result.toClient(0, "公司介绍", introduce, introduce.size());
		PrintWriter out = response.getWriter();
		out.write(res);
		out.flush();
		out.close();
	}

	// 获取“设计师”信息
	public void dosheji(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取设计师图片的列表
		ArrayList<DesignerEntity> desingner = dao.designer();

		String res = Result.toClient(0, "公司介绍", desingner, desingner.size());
		PrintWriter out = response.getWriter();
		out.write(res);
		out.flush();
		out.close();
	}
	public void doliuyan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取设计师图片的列表
		ArrayList<MessaGeEntity> desingner = dao.adminmessage();

		String res = Result.toClient(0, "liuyan", desingner, desingner.size());
		PrintWriter out = response.getWriter();
		out.write(res);
		out.flush();
		out.close();
	}
//添加设计师
	public void intsertsheji(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String position = request.getParameter("position");
		String url = request.getParameter("url2");
		String xiuTime = request.getParameter("xiuTime2");
		boolean aa = dao.addsheji(new DesignerEntity(0, name, sex, position, url, xiuTime));
		PrintWriter out = response.getWriter();
		if (aa) {
			out.write(Result.toClient(0, "成功"));
		} else {
			out.write(Result.toClient(1, "失败"));
		}
		out.flush();
		out.close();
		// request.getRequestDispatcher("about.jsp").forward(request, response);
	}

	// 设计师修改
	public void updatesheji(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id2 = request.getParameter("id");
		int id = Integer.valueOf(id2);
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String position = request.getParameter("position");
		String url = request.getParameter("url");
		String xiuTime = request.getParameter("xiuTime");
		boolean aa = dao.updatesheji(new DesignerEntity(id, name, sex, position, url, xiuTime));
		PrintWriter out = response.getWriter();
		if (aa) {
			out.write(Result.toClient(0, "成功"));
		} else {
			out.write(Result.toClient(1, "失败"));
		}
		out.flush();
		out.close();
	}
	public void updateliuyan(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id2 = request.getParameter("id");
		int id = Integer.valueOf(id2);
		String name = request.getParameter("name");
		String telephone = request.getParameter("telephone");
		String email = request.getParameter("email");
		String contentt = request.getParameter("contentt");
		String text = request.getParameter("text");
		boolean aa = dao.updateliuyan(new MessaGeEntity(id,name, telephone, email, contentt, text));
		PrintWriter out = response.getWriter();
		if (aa) {
			out.write(Result.toClient(0, "成功"));
		} else {
			out.write(Result.toClient(1, "失败"));
		}
		out.flush();
		out.close();
	}
	// 设计师删除
	public void deletesheji(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id2 = request.getParameter("id");
		int id = Integer.valueOf(id2);
		System.out.println(id);
		boolean aa = dao.delsheji(id);
		PrintWriter out = response.getWriter();
		if (aa) {
			out.write(Result.toClient(0, "成功"));
		} else {
			out.write(Result.toClient(1, "失败"));
		}
		out.flush();
		out.close();
	}

//留言板的添加方法
	public void getmessage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name = request.getParameter("name");
		String telephone = request.getParameter("telephone");
		String email = request.getParameter("email");
		String contentt = request.getParameter("contentt");
		String text = request.getParameter("text");
		boolean res = dao.addmessage(new MessaGeEntity(1000, name, telephone, email, contentt,text));

		if (res == true) {
			response.sendRedirect("message.jsp");

		}
	}

//上传图片
	public void uploadImg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		// 1.创建SmartUpload
		SmartUpload smart = new SmartUpload();
		// 2.设置字符集
		smart.setCharset("utf-8");
		// 3.初始化
		smart.initialize(getServletConfig(), request, response);

		try {
			// 4.上传文件
			smart.upload();

			// 5.保存文件到指定位置(save只提供路径，使用原文件名上传文件)
			smart.save("/images");
			// 获取文件名
			String filename = smart.getFiles().getFile(0).getFileName();
			// 将数据进行返回
			out.print(Result.toClient(0, "新图片上传成功", filename));

		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// 将数据进行返回
			out.print(Result.toClient(1, "新图片上传失败"));
		}
		out.flush();
		out.close();
	}
}
